﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media.Effects;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System.IO;

namespace TouchUmlUI
{

    public class NotatkaObj : AssociableObj
    {
        public TextBlock cntLabel;        
        Binding bind1;
        Binding bind2;
        Image pin;
        private Polygon poly;
        private Polygon corner;
        double mwidth;
        double mheight;
        Rectangle r;


        public override void showHints(bool a_show)
        {
            int thick = a_show ? 1 : 0;
            r.StrokeThickness = thick;        
        }

        public override void updateDependencyProperties()
        {                       
            if (guml.panel.ActualWidth != 0)
            {
                mwidth = guml.panel.ActualWidth;
                mheight = guml.panel.ActualHeight;
            }

            poly.Points.Clear();
            poly.Points.Add(new Point(0,0));
            double sX = (mwidth / 250.0f) * 42;
            double sY = (mheight / 250.0f) * 42;
            if (sX > 42) sX = 42;
            if (sY > 42) sY = 42;
            poly.Points.Add(new Point(0,mheight - sY));
            poly.Points.Add(new Point(sX, mheight));
            poly.Points.Add(new Point(mwidth, mheight));
            poly.Points.Add(new Point(mwidth, 0));           

            corner.Points.Clear();
            corner.Fill = new SolidColorBrush(Color.FromRgb(255, 231, 165));
            corner.Stroke = poly.Stroke;
            corner.StrokeThickness = poly.StrokeThickness;
            corner.Points.Add(new Point(0, mheight - sY));
            corner.Points.Add(new Point(sX * 0.4, mheight - sY - 4));
            corner.Points.Add(new Point(sX, mheight));            

            Canvas.SetLeft(pin, (mwidth) / 2.0f);
            Canvas.SetTop(pin, -3);

            if (mwidth > 20)
                r.Width = mwidth - 20;
            else
                r.Width = 20;

            if (mheight > 20)
                r.Height = mheight - 60;
            else
                r.Height = 20;

            Canvas.SetLeft(r, 10);
            Canvas.SetTop(r,10);

            cntLabel.Text = ((Diagram.Notatka)guml.obj_uml).Tekst;
            cntLabel.UpdateLayout();
            Canvas.SetLeft(cntLabel, 30);
            Canvas.SetTop(cntLabel, 30);

            base.updateDependencyProperties();
        }

        public NotatkaObj(String a_title, int a_width, int a_height, int a_left, int a_top, OurCanvas a_canvas, int a_id, GUML guml)
            : base(guml)
        {

            poly = new Polygon();
            corner = new Polygon();
            guml.canvas = a_canvas;
            setZOrder(a_id);


            if (a_width < 0) { a_left = a_left + a_width; a_width = -a_width; }
            if (a_height < 0) { a_top = a_top + a_height; a_height = -a_height; }

            x = a_left;
            y = a_top;
            width = a_width;
            height = a_height;
            setWidth(width);
            setHeight(height);
            guml.panel = new Canvas();
            guml.panel.MinWidth = 100;
            guml.panel.MinHeight = 100;
            InkCanvas.SetLeft(guml.panel, x);
            InkCanvas.SetTop(guml.panel, y);
            guml.panel.Width = width;
            guml.panel.Height = height;
            a_canvas.Children.Add(guml.panel);                       
            double sX = (width / 300.0f) * 42;
            double sY = (height / 300.0f) * 42;
            poly.Points.Add(new Point(0, 0));
            poly.Points.Add(new Point(0, height - sY));
            poly.Points.Add(new Point(sX, height));
            poly.Points.Add(new Point(width, height));
            poly.Points.Add(new Point(width, 0));          
            poly.Fill = new SolidColorBrush(Color.FromRgb(255, 255, 223));
            poly.Stroke = new SolidColorBrush(Color.FromRgb(249,213,161));
            poly.StrokeThickness = 1;
            Canvas.SetLeft(poly, 0);
            Canvas.SetTop(poly, 0);
           

            BitmapImage sourcePin;
            sourcePin = new BitmapImage();
            sourcePin.BeginInit();
            sourcePin.UriSource = new Uri(Directory.GetCurrentDirectory() + "\\ikony\\pin.png");
            sourcePin.EndInit();
            pin = new Image();
            pin.Source = sourcePin;
            pin.Width = 15;
            pin.Height = 21;
              
            Canvas.SetLeft(pin, a_width / 2.0f);
            Canvas.SetTop(pin, -3);

            r = new Rectangle();
            if (width - 50 > 20)
                r.Width = width - 50;
            else
                r.Width = 20;

            if (width - 60 > 20)
                r.Height = height - 60;
            else
                r.Height = 20;

            
            r.Stroke = Brushes.Black;
            r.StrokeThickness = 1;
            r.StrokeDashCap = PenLineCap.Square;
            r.StrokeDashArray.Add(5);
            r.Fill = new SolidColorBrush(Color.FromArgb(30, 255, 255, 255));
            r.Opacity = 0.2;
            r.MouseDown += new MouseButtonEventHandler(r_MouseDown);


            guml.panel.Children.Add(poly);
            guml.panel.Children.Add(corner);
            guml.panel.Children.Add(pin);
            guml.panel.Children.Add(r);
            

            cntLabel = new TextBlock();
            cntLabel.Text = " " + "...";
            Canvas.SetLeft(cntLabel, 0);
            Canvas.SetTop(cntLabel, 35);           
            guml.panel.Children.Add(cntLabel);
            
            bind1 = new Binding("WidthProperty");
            bind1.Source = guml.panel;
            bind1.Path = new PropertyPath(Canvas.WidthProperty);
            bind2 = new Binding("HeightProperty");
            bind2.Source = guml.panel;
            bind2.Path = new PropertyPath(Canvas.HeightProperty);                                    
            cntLabel.SetBinding(Label.WidthProperty, bind1);
            guml.panel.UpdateLayout();
            Refresh();                        
            updateDependencyProperties();
        }

        void r_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (SignalR.windowParent.handWrite != null) return;
            SignalR.windowParent.handWrite = new UCUTextRecognizer.HandWrite();
            UCUTextRecognizer.HandWrite handWrite = SignalR.windowParent.handWrite;
            handWrite.InitializeComponent();

            try
            {
                if (!((guml.obj_uml as Diagram.Notatka).Tekst).StartsWith("Notatka"))
                {
                    handWrite.setExistingText((guml.obj_uml as Diagram.Notatka).Tekst);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
            double sy = getY() + 10;
            double sx = SignalR.windowParent.sv.HorizontalOffset + 50;
            double nw = SignalR.windowParent.sv.ActualWidth - 100;
            double nh = 150;

            handWrite.Width = nw;
            handWrite.Height = nh;

            InkCanvas.SetLeft(handWrite, sx);
            InkCanvas.SetTop(handWrite, sy);
            handWrite.Opacity = 1.0f;
            //
            handWrite.bindWithObject(guml.obj_uml, guml.canvas, guml.umlobj);
            SignalR.windowParent.active_canvas.Children.Add(handWrite);
            e.Handled = true;
        }

        public override void Refresh()
        {
            //cntLabel.Width = width;
            //cntLabel.Text = ((Diagram.Notatka)guml.obj_uml).Tekst;
        }

        public override void setX(int ax)
        {
            x = ax;
            InkCanvas.SetLeft(guml.panel, x);
        }

        public override void setY(int ay)
        {
            y = ay;
            InkCanvas.SetTop(guml.panel, y);
        }

        public override void setWidth(int a_width)
        {
            mwidth = a_width;            
            width = a_width;            
        }
        public override void setHeight(int a_height)
        {
            mheight = a_height;
            height = a_height;            
        }


    }

}
